package ai.people.netmon.userconfig.controller;

import ai.people.netmon.framework.domain.userconfig.entity.UserCollectConfigKeywords;
import ai.people.netmon.framework.domain.userconfig.request.KeywordsQueryRequest;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.userconfig.service.UserCollectConfigKeywordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 采集配置关键词接口
 *
 * @author yuanqinglong
 * @date 2022/2/28 10:11
 */
@Validated
@RestController
@RequestMapping("/keywords")
public class KeywordsController {
    @Autowired
    UserCollectConfigKeywordsService service;

    /**
     * 分页查询接口
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link UserCollectConfigKeywords}>>
     */
    @PostMapping("/page")
    Result<PageResponse<UserCollectConfigKeywords>> page(@Valid @RequestBody KeywordsQueryRequest request){
        PageResponse<UserCollectConfigKeywords> response = service.pageQuery(request);
        return ResultUtil.success(response);
    }

    /**
     * 通过id获取关键词
     *
     * @param id id
     * @return {@link Result}<{@link UserCollectConfigKeywords}>
     */
    @RequestMapping("/{id}")
    Result<UserCollectConfigKeywords> getAccountById(@PathVariable("id")Long id){
        UserCollectConfigKeywords account = service.getKeywordsById(id);
        return ResultUtil.success(account);
    }

    /**
     * 保存关键词
     *
     * @param keywords 关键词
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/save")
    Result<Boolean> saveAccount(@Valid @RequestBody UserCollectConfigKeywords keywords){
        service.saveKeywords(keywords);
        return ResultUtil.success();
    }

    /**
     * 更新关键词
     *
     * @param keywords 关键词
     * @return {@link Result}<{@link Boolean}>
     */
    @PutMapping
    Result<Boolean> updateAccount(@RequestBody UserCollectConfigKeywords keywords){
        if (keywords == null || keywords.getId() == null) {
            return ResultUtil.error(CommonEnum.CHECK_NOT_EMPTY_PRIMARY_ID);
        }
        service.updateKeywords(keywords);
        return ResultUtil.success();
    }

    /**
     * 删除关键词
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @DeleteMapping("{id}")
    Result<Boolean> delAccount(@PathVariable("id")  Long id){
        service.delKeywordsById(id);
        return ResultUtil.success();
    }

}
